package com.dwjm.exam.stu_login;

import com.dwjm.exam.pojo.Student;
import com.dwjm.utils.ConnectionDatabaseForMySQLUseJdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginDaoImpl implements LoginDao {
    @Override
    public Student studentLogin(String username, String password) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Student student = null;

        try {
            conn = ConnectionDatabaseForMySQLUseJdbc.getConnection();
            String sql = "SELECT * FROM student WHERE username = ? AND password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                student = new Student();
                student.setStudentId(rs.getString("studentId"));
                student.setName(rs.getString("name"));
                student.setGender(rs.getString("gender"));
                student.setBirthDate(rs.getDate("birthDate"));
                student.setIdCard(rs.getString("idCard"));
                student.setCollege(rs.getString("college"));
                student.setMajor(rs.getString("major"));
                student.setClassName(rs.getString("className"));
                student.setStatus(rs.getString("status"));
                student.setDormId(rs.getString("dormId"));
                student.setBedNumber(rs.getString("bedNumber"));
                student.setPhone(rs.getString("phone"));
                student.setEmail(rs.getString("email"));
                student.setEmergencyContact(rs.getString("emergencyContact"));
                student.setUsername(rs.getString("username"));
                student.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 修正参数顺序：ResultSet, Statement, Connection
            ConnectionDatabaseForMySQLUseJdbc.close(conn, pstmt, rs);
        }

        return student;
    }


    @Override
    public boolean adminLogin(String username, String password) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        boolean result = false;

        try {
            conn = ConnectionDatabaseForMySQLUseJdbc.getConnection();
            String sql = "SELECT * FROM administrator WHERE username = ? AND password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                result = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionDatabaseForMySQLUseJdbc.close(conn, pstmt, rs);
        }

        return result;
    }
}